package lib.bone.admin.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lib.bone.admin.anno.WebLog;
import lib.bone.admin.constant.MethodTypeConst;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.system.domain.dto.role.RoleAddDTO;
import lib.bone.system.domain.dto.role.RoleQueryDTO;
import lib.bone.system.domain.dto.role.RoleUpdateDTO;
import lib.bone.system.domain.entity.SysRole;
import lib.bone.system.service.SysMenuService;
import lib.bone.system.service.SysRoleMenuService;
import lib.bone.system.service.SysRoleService;
import lib.bone.system.service.SysUserRoleService;
import lib.bone.system.service.impl.SysMenuServiceImpl;
import lib.bone.system.service.impl.SysRoleMenuServiceImpl;
import lib.bone.system.service.impl.SysRoleServiceImpl;
import lib.bone.system.service.impl.SysUserRoleServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author kindear
 */
@Slf4j
@RestController
@RequestMapping("system")
@Api(tags = "系统角色管理")
public class SysRoleController {

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysMenuService sysMenuService;

    @Autowired
    SysRoleMenuService sysRoleMenuService;

    @Autowired
    SysUserRoleService sysUserRoleService;

    @WebLog(module = "bone-system")
    @PostMapping("roles")
    @ApiOperation(value = "新增角色",notes = "")
    @SaCheckRole("admin")
    public ResponseDTO saveRole(@RequestBody @Validated RoleAddDTO roleAddDTO){
        return sysRoleService.saveOne(roleAddDTO);
    }

    @WebLog(module = "bone-system",operate = MethodTypeConst.UPDATE)
    @PutMapping("roles")
    @ApiOperation(value = "更新角色",notes = "")
    @SaCheckRole("admin")
    public ResponseDTO updateRole(@RequestBody @Validated RoleUpdateDTO roleUpdateDTO){
        return sysRoleService.updateOne(roleUpdateDTO);
    }

    @GetMapping("roles/{roleId}")
    @ApiOperation(value = "获取单个角色")
    @SaCheckPermission(value = "system_role:list",orRole = "admin")
    public ResponseDTO getOneRole(@PathVariable("roleId") Long roleId){
        Optional<SysRole> sysRoleOp = sysRoleService.getById(roleId);
        if(!sysRoleOp.isPresent()){
            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"ID:["+roleId+"]对应角色不存在！");
        }
        return ResponseDTO.succData(sysRoleOp.get());
    }

    @WebLog(module = "bone-system",operate = MethodTypeConst.DELETE)
    @DeleteMapping("roles/{roleId}")
    @ApiOperation(value = "删除单个角色",notes = "")
    @SaCheckRole("admin")
    public ResponseDTO deleteRole(@PathVariable("roleId") Long roleId){
        return sysRoleService.deleteById(roleId);
    }

    @GetMapping("roles")
    @ApiOperation(value = "获取全部角色",notes = "根据传入参数，获取角色信息")
    @SaCheckPermission(value = "system_role:list",orRole = "admin")
    public ResponseDTO<PageResultDTO<SysRole>> getRoles(RoleQueryDTO roleQueryDTO){

        Sort sort;
        if(roleQueryDTO.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, roleQueryDTO.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, roleQueryDTO.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(roleQueryDTO.getPageNum() - 1 , roleQueryDTO.getPageSize(), sort);

        SysRole sysRole = new SysRole();
        BeanUtil.copyProperties(roleQueryDTO,sysRole, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<SysRole> example = Example.of(sysRole);
        return ResponseDTO.succData(sysRoleService.getAll(pageable,example));
    }


    /**
     * 修改对应角色状态
     */
    @WebLog(module = "bone-system",operate = MethodTypeConst.UPDATE)
    @PutMapping("roles/{roleId}/status/{status}")
    @ApiOperation(value = "更新角色状态",notes = "")
    @SaCheckRole("admin")
    public ResponseDTO changeRoleStatus(@PathVariable("roleId") Long roleId,@PathVariable("status") Integer status){
        return sysRoleService.changeRoleStatus(roleId,status);
    }

}
